package com.aliyun.messagemgt.repository.foura;

import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserEmployeeResVO;
import com.aliyun.messagemgt.common.dto.response.ResultMap;
import com.aliyun.messagemgt.domain.repository.FouraRepository;
import com.aliyun.messagemgt.domain.repository.IBaseTenantUserRepository;
import com.aliyun.messagemgt.dto.base.BaseTenantUserDTO;
import com.aliyun.messagemgt.entities.foura.FourAUserResponse;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.filter.XxlSsoProperties;
import com.xxl.sso.core.user.XxlSsoUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class FouraServiceMybatisRepositoryNew implements FouraRepository {
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private IBaseTenantUserRepository tenantUserRepo;
    ;
    @Autowired
    private XxlSsoProperties xxlSsoProperties;


    /**
     * 组装登录pageURL
     *
     * @param req
     * @return
     */
    private String getLoginPageUrl(HttpServletRequest req) {
        String ssoServer = xxlSsoProperties.getServer();
        String loginPageUrl = ssoServer.concat(Conf.SSO_LOGOUT)
                + "?" + Conf.REDIRECT_URL + "=" ;

//        String loginPageUrl = StringUtils.isEmpty(this.loginPageUrl) ?
//                "http://uc-sso-feature.ingress.dayu.work/sso-server/static/login.html?redirect_url=http://message-pc-fe-feature.ingress.dayu.work/mgt/sso/receive&to_url=http://message-pc-fe-feature.ingress.dayu.work/"
//                : this.loginPageUrl;
        return loginPageUrl;
    }

    @Override
    public ResultMap getUserAndExtUserCode(HttpServletRequest request) {
        HashMap<String, String> map = new HashMap<>(1);

        map.put("loginPageUrl", getLoginPageUrl(request));
        //1. 用session拿userid

        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if (ObjectUtils.isEmpty(xxlSsoUser)) {
            return ResultMap.error(HttpStatus.NOT_IMPLEMENTED.value(), "请使用正确的用户登录！", map);
        }
        String userid = xxlSsoUser.getUserid();
        if (StringUtils.isEmpty(userid)) {
            return ResultMap.error(HttpStatus.NOT_IMPLEMENTED.value(), "请使用正确的用户登录！", map);
        }
        //检查租户信息
        List<BaseTenantUserDTO> tenantUsers = tenantUserRepo.getBaseTenantUserDTOByUserId(userid);
        if (ObjectUtils.isEmpty(tenantUsers)) {
            return ResultMap.error(HttpStatus.NOT_IMPLEMENTED.value(), "请使用正确的用户登录！", map);
        }
        Long userId = Long.valueOf(userid);
        //2. 用userId拿用户信息
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setUserId(userId);
        RpcUserInfoVO rpcUserInfoVO = userFeignService.qryUserInfo(selectUserInfoReqVO).getData();
        //3. 类转换
        FourAUserResponse fourAUserResponse = new FourAUserResponse();
        UserEmployeeResVO employeeInfo = rpcUserInfoVO.getEmployeeInfo();
        if (!ObjectUtils.isEmpty(employeeInfo)) {
            fourAUserResponse.setUserCode(employeeInfo.getEmployeeCode());
        }
        fourAUserResponse.setUserName(rpcUserInfoVO.getUserName());
        fourAUserResponse.setPhone(rpcUserInfoVO.getMobilePhone());
        fourAUserResponse.setEmail(rpcUserInfoVO.getEmail());
        fourAUserResponse.setSex("1");
        fourAUserResponse.setUserId("" + userId);
        //String jsonText="{\"orgId\":\"1919f623b5d140598b4eb80660715055\",\"orgCode\":\"17801130126\",\"userStatus\":null,\"userCode\":\"17801130126\",\"userName\":\"超级管理员\",\"userId\":\"17801130126\",\"phone\":\"17801130126\",\"departmentName\":\"BJZG\",\"sex\":\"2\",\"position\":null,\"email\":\"\",\"isAdmin\":false}";
        //FourAUserResponse fourAUserResponse=JSONObject.parseObject(jsonText,FourAUserResponse.class);
        return ResultMap.success(fourAUserResponse);
    }

}
